package com.qubi.exchange.protobuffer;

import com.qubi.exchange.websocket.DepthNotifyMessage;
import com.qubi.exchange.websocket.OrderNotifyEntity;
import com.qubi.me.entity.ColaCoinSymbol;

import java.math.BigDecimal;
import java.util.List;
import java.util.Set;

public class BufferTransfrom {
    public static class MarketTransform {
        public static byte[] getMarketBuffer(String topic, com.qubi.exchange.data.MarketInfo info) {
            MarketBuffer.MarketInfo.Builder marketInfoBuilder = MarketBuffer.MarketInfo.newBuilder();
            marketInfoBuilder.setGain24(info.getGain_24().floatValue());
            marketInfoBuilder.setIcon(info.getIcon());
            marketInfoBuilder.setPair(info.getPair());
            marketInfoBuilder.setPrice(info.getPrice().doubleValue());
            marketInfoBuilder.setMax24H(info.getMax_24h().doubleValue());
            marketInfoBuilder.setMin24H(info.getMin_24h().doubleValue());
            marketInfoBuilder.setVol(info.getVol().doubleValue());
            marketInfoBuilder.setSort(info.getSort());
            marketInfoBuilder.setTopic(topic);
            return marketInfoBuilder.build().toByteArray();
        }

        public static byte[] getInstance(String topic, List<ColaCoinSymbol> coinSymbol, List<com.qubi.exchange.data.MarketInfo> marketInfo) {

            MarketBuffer.marketInfoList.Builder builder = MarketBuffer.marketInfoList.newBuilder();
            for (ColaCoinSymbol colaCoinSymbol : coinSymbol) {
                MarketBuffer.Symbol.Builder symbolInfoBuilder = MarketBuffer.Symbol.newBuilder();
                symbolInfoBuilder.setAllowBuy(colaCoinSymbol.getAllowBuy());
                symbolInfoBuilder.setAllowSell(colaCoinSymbol.getAllowSell());
                symbolInfoBuilder.setAmountScale(colaCoinSymbol.getAmountScale());
                symbolInfoBuilder.setPriceScale(colaCoinSymbol.getPriceScale());
                symbolInfoBuilder.setBuyOpenTime(symbolInfoBuilder.getBuyOpenTime());
                symbolInfoBuilder.setSellOpenTime(colaCoinSymbol.getSellOpenTime());
                symbolInfoBuilder.setIcon("");
                symbolInfoBuilder.setCoinCode(colaCoinSymbol.getCoinCode());
                symbolInfoBuilder.setFees(colaCoinSymbol.getFees().floatValue());
                symbolInfoBuilder.setSymbol(colaCoinSymbol.getSymbol());
                symbolInfoBuilder.setBuyMaxNumber(colaCoinSymbol.getBuyMaxNumber().doubleValue());
                symbolInfoBuilder.setSellMaxNumber(colaCoinSymbol.getSellMaxNumber().doubleValue());
                symbolInfoBuilder.setBuyMinNumber(colaCoinSymbol.getBuyMinNumber().doubleValue());
                symbolInfoBuilder.setSellMinNumber(colaCoinSymbol.getSellMinNumber().doubleValue());
                builder.addSymbol(symbolInfoBuilder.build());

            }

            for (com.qubi.exchange.data.MarketInfo info : marketInfo) {
                MarketBuffer.MarketInfo.Builder marketInfoBuilder = MarketBuffer.MarketInfo.newBuilder();
                marketInfoBuilder.setGain24(info.getGain_24().floatValue());
                marketInfoBuilder.setIcon(info.getIcon());
                marketInfoBuilder.setPair(info.getPair());
                marketInfoBuilder.setPrice(info.getPrice().doubleValue());
                marketInfoBuilder.setMax24H(info.getMax_24h().doubleValue());
                marketInfoBuilder.setMin24H(info.getMin_24h().doubleValue());
                marketInfoBuilder.setVol(info.getVol().doubleValue());
                marketInfoBuilder.setSort(info.getSort());
                builder.addMarketInfo(marketInfoBuilder.build());

            }
            builder.setTopic(topic);
            return builder.build().toByteArray();


        }
    }

    public static class DepthTransform{

        public static byte[] getBuffer(String topic, DepthNotifyMessage depthNotifyMessage)
        {
            DepthBuffer.DepthList.Builder builder= DepthBuffer.DepthList.newBuilder();
            for(BigDecimal price:depthNotifyMessage.getAsk().keySet()){
                DepthBuffer.DepthEntity.Builder b= DepthBuffer.DepthEntity.newBuilder();
                b.setPrice(price.floatValue());
                b.setNumber(depthNotifyMessage.getAsk().get(price).doubleValue());
                builder.addAsks(b.build());
            }
            for(BigDecimal price:depthNotifyMessage.getBids().keySet()){
                DepthBuffer.DepthEntity.Builder b= DepthBuffer.DepthEntity.newBuilder();
                b.setPrice(price.floatValue());
                b.setNumber(depthNotifyMessage.getBids().get(price).doubleValue());
                builder.addBids(b.build());
            }
            builder.setTopic(topic);
            return builder.build().toByteArray();
        }
        public static byte[] getBuffer(String topic, List<BigDecimal[]> ask,List<BigDecimal[]>bid,BigDecimal current)
        {
            DepthBuffer.DepthList.Builder builder= DepthBuffer.DepthList.newBuilder();
            for(BigDecimal[] price:ask){
                DepthBuffer.DepthEntity.Builder b= DepthBuffer.DepthEntity.newBuilder();
                b.setPrice(price[0].floatValue());
                b.setNumber(price[1].doubleValue());
                builder.addAsks(b.build());
            }
            for(BigDecimal[] price:bid){
                DepthBuffer.DepthEntity.Builder b= DepthBuffer.DepthEntity.newBuilder();
                b.setPrice(price[0].floatValue());
                b.setNumber(price[1].doubleValue());
                builder.addBids(b.build());
            }
            builder.setCurrent(current.floatValue());
            builder.setTopic(topic);
            return builder.build().toByteArray();
        }



    }
    public static class KlinesTransform{
        public static byte[] getBuffer(List<Number[]> numbers,String topic)
        {  KlinesBuffer.Kline.Builder klineBuilder= KlinesBuffer.Kline.newBuilder();
            KlinesBuffer.KlineList.Builder klinesListBuilder= KlinesBuffer.KlineList.newBuilder();
            for(Number[]n:numbers){
                klineBuilder.setTime((Long) n[0]);
                klineBuilder.setOpen(Float.parseFloat(n[1].toString()));
                klineBuilder.setHigh(Float.parseFloat(n[2].toString()));
                klineBuilder.setLow(Float.parseFloat(n[3].toString()));
                klineBuilder.setClose(Float.parseFloat(n[4].toString()));
                klineBuilder.setVol(Double.parseDouble(n[5].toString()));
                klinesListBuilder.addKline(klineBuilder.build());
            }
            klinesListBuilder.setTopic(topic);
            return klinesListBuilder.build().toByteArray();

        }


    }
    public  static class OrderTransform{

        public  static byte[]getBuffer(String topic, List<OrderNotifyEntity> orderNotifyEntities) {
            OrderBuffer.OrderList.Builder builder=OrderBuffer.OrderList.newBuilder();
            for(OrderNotifyEntity orderNotifyEntity:orderNotifyEntities){
                OrderBuffer.OrderEntity.Builder b= OrderBuffer.OrderEntity.newBuilder();
                b.setDirection(orderNotifyEntity.getDirection());
                b.setNumber(orderNotifyEntity.getNumber().doubleValue());
                b.setPrice(orderNotifyEntity.getPrice().floatValue());
                b.setTimestamp(orderNotifyEntity.getTimestamp());
                builder.addOrders(b.build());
            }
            builder.setTopic(topic);
            return builder.build().toByteArray();
        }
    }

    public static class PersonOrder{


        public static byte[] getBuffer(String topic, Set<String> pairs) {
            PersonOrderBuffer.PersonOrderList.Builder builder = PersonOrderBuffer.PersonOrderList.newBuilder();
            builder.addAllPairs(pairs);
            builder.setTopic(topic);
            return builder.build().toByteArray();


        }
    }
}
